package com.webserver.http;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示一个响应，每个响应由三部分构成
 *
 * 1：状态行
 * 2：响应头
 * 3：响应正文
 */
public class HttpServletResponse {
    //状态行相关信息
    private Socket socket;
    private int statusCode =200;
    private String statusReason ="ok";
    //响应头相关信息
    private Map<String,String> headers = new HashMap<>();


    //响应正文相关信息
    private File entity;

    public HttpServletResponse(Socket socket) {
        this.socket = socket;
    }

    public void response() throws IOException {
        OutputStream out = socket.getOutputStream();
        /*
        1发送状态行
         */
        sendStatusLine();
        /*
        2发送响应头
         */
        sendHeaders();
        /*
        3送响应正文
         */
        sendContent();
        System.out.println("响应发送完毕");

    }
    /*
    发送状态行
     */
    private void sendStatusLine() throws IOException {

        String line =  "HTTP/1.1"+" "+statusCode+" "+statusReason;
       println(line);
    }
    /*
    发送响应头
     */
    private void sendHeaders() throws IOException {
        Set<Map.Entry<String,String>> entrySet = headers.entrySet();
        for (Map.Entry<String,String> e : entrySet){
            String key = e.getKey();
            String value = e.getValue();
            String line = key+": "+value;
            println(line);
        }
       println("");
    }
    /*
    发送响应正文
     */
    private void sendContent() throws IOException {

        OutputStream out = socket.getOutputStream();
        if (entity!=null) {
            try(
            FileInputStream fis = new FileInputStream(entity);
            ) {
                int len;
                byte[] buf = new byte[1024 * 10];
                while ((len = fis.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
            }
        }
    }
    private void println (String line) throws IOException {
        byte[] data = line.getBytes("ISO8859-1");
        OutputStream out = socket.getOutputStream();
        out.write(data);
        out.write(13);
        out.write(10);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {
        this.entity = entity;
        String fileName =entity.getName();
        String ext = fileName.substring(fileName.lastIndexOf('.')+1);
        String type = HttpContext.getMineType(ext);
        addHeader("Content-Type",type);
        addHeader("Content-Length",String.valueOf(entity.length()));

    }
    /*
    添加一个要发送的响应头
     */

    public void addHeader(String name,String value){
        this.headers.put(name,value);
    }
}
